/*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */

package SharedDrawing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class SaveAllPanel extends JDialog{
    
    JTable table;
    JFileChooser chooser;
    JButton saveButton;
    JButton dontButton;
    JButton cancelButton;
    private boolean status;
    JPanel myPanel;
    
    public SaveAllPanel(final JTable table, JFileChooser fileChooser, final JFrame frame) {
        super(frame, true);
        final SaveAllPanel me = this;
        myPanel = new JPanel();
        getContentPane().add(myPanel);
        status = false;
        this.table = table;
        this.chooser = fileChooser;
        myPanel.setLayout(new BorderLayout());
        saveButton = new JButton("save");
        dontButton = new JButton("don't save");
        cancelButton = new JButton("cancel");
        //north
        myPanel.add(new JLabel("do you want to save ?"), BorderLayout.NORTH);
        Box south = Box.createHorizontalBox();
        //south
        south.add(Box.createGlue());
        south.add(saveButton);
        saveButton.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e)
            {
                chooser.setApproveButtonText("Save");
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int returnVal = chooser.showOpenDialog(frame);
                if(returnVal == JFileChooser.APPROVE_OPTION)
                {   
                    File file = chooser.getSelectedFile();
                    List<Page> pages = new ArrayList<Page>();
                    for(Integer i = 0; i<table.getRowCount();i++)
                    {
                        if((Boolean)table.getValueAt(i, 1))
                        {
                            pages.add(Pages.getInstance().getPage((String)table.getValueAt(i, 0)));
                        }
                        if((Boolean)table.getValueAt(i, 2))
                        {
                            Pages.getInstance().getPage((String)table.getValueAt(i, 0)).
                                toPNG(file.getParent()+"/"+(String)table.getValueAt(i, 0)+".png");
                        }
                    }
                    XmlFile.getInstance().save(file,pages);
                    me.setVisible(false);
                    status = true;
                }
            }
        });
        south.add(dontButton);
        dontButton.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e)
            {
                me.setVisible(false);
                status = true;
            }
        });
        south.add(cancelButton);
        cancelButton.addActionListener(new ActionListener() {
 
            @Override
            public void actionPerformed(ActionEvent e)
            {
                me.setVisible(false);
                status = false;
            }
        });
        myPanel.add(south, BorderLayout.SOUTH);
        //center
         // clear the list of pages
        table.removeAll();
        //add all pages
        for(Integer i : Pages.getInstance().getPages().keySet())
        {
            DefaultTableModel currentRow =(DefaultTableModel)table.getModel();
            Object[] rowData = new Object[3]; 
            rowData[0] = Pages.getInstance().getPages().get(i).getName();
            rowData[1] = true; 
            rowData[2] = false; 
            currentRow.addRow(rowData);
        }
        JScrollPane tableContainer = new JScrollPane(table);
        myPanel.add(tableContainer, BorderLayout.CENTER);
        //show
        setPreferredSize(getPreferredSize());
        pack();
        setLocationRelativeTo(frame);
        setVisible(true);
    }
    
    
    
    public boolean run(){
        return status;
    }
}
